﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using 多协议通信平台.Entity;
using 多协议通信平台.Models;

namespace 多协议通信平台.Views.Dialog
{
    /// <summary>
    /// ModbusPointEditDialog.xaml 的交互逻辑
    /// </summary>
    public partial class ModbusPointEditDialog : UserControl
    {
        public ModbusPointEditDialog()
        {
            InitializeComponent();
            InitDic();
        }

        private void InitDic()
        {
            DataTypes.Add("Bit", "bool");
            DataTypes.Add("short", "Int16");
            DataTypes.Add("ushort", "UInt16");
            DataTypes.Add("int", "Int32");
            DataTypes.Add("float", "Single");
            DataTypes.Add("long", "Int64");
        }

        public string DialogTitle
        {
            get { return (string)GetValue(DialogTitleProperty); }
            set { SetValue(DialogTitleProperty, value); }
        }

        public static readonly DependencyProperty DialogTitleProperty =
            DependencyProperty.Register("DialogTitle", typeof(string), typeof(ModbusPointEditDialog), new PropertyMetadata("设置通信点位"));


        public Dictionary<string, string> DataTypes { get; set; } = new Dictionary<string, string>();


        // Eidt编辑地址，Write写入值
        public string Purpose
        {
            get { return (string)GetValue(PurposeProperty); }
            set { SetValue(PurposeProperty, value); }
        }

        public static readonly DependencyProperty PurposeProperty =
            DependencyProperty.Register("Purpose", typeof(string), typeof(ModbusPointEditDialog), new PropertyMetadata("Edit"));



        public ModbusPoint ModbusPoint
        {
            get { return (ModbusPoint)GetValue(ModbusPointProperty); }
            set { SetValue(ModbusPointProperty, value); }
        }

        public static readonly DependencyProperty ModbusPointProperty =
            DependencyProperty.Register("ModbusPoint", typeof(ModbusPoint), typeof(ModbusPointEditDialog), new PropertyMetadata(null));


        public ICommand SaveSetCommand
        {
            get { return (ICommand)GetValue(SaveSetCommandProperty); }
            set { SetValue(SaveSetCommandProperty, value); }
        }

        public static readonly DependencyProperty SaveSetCommandProperty =
            DependencyProperty.Register("SaveSetCommand", typeof(ICommand), typeof(ModbusPointEditDialog), new PropertyMetadata(null));


        public ICommand WriteValCommand
        {
            get { return (ICommand)GetValue(WriteValCommandProperty); }
            set { SetValue(WriteValCommandProperty, value); }
        }

        public static readonly DependencyProperty WriteValCommandProperty =
            DependencyProperty.Register("WriteValCommand", typeof(ICommand), typeof(ModbusPointEditDialog), new PropertyMetadata(null));


        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Collapsed;
        }

        private void Confirm_Save_Click(object sender, RoutedEventArgs e)
        {
            SaveSetCommand?.Execute(ModbusPoint);
            this.Visibility = Visibility.Collapsed;
        }

        private void Confirm_Write_Click(object sender, RoutedEventArgs e)
        {
            WriteValCommand?.Execute(ModbusPoint);
            this.Visibility = Visibility.Collapsed;
        }

        private void Close_Button_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Collapsed;
        }

        private void ComboBox_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Enter)
            {
                var comboBox = sender as HandyControl.Controls.ComboBox;
                comboBox.IsDropDownOpen = true;
                comboBox.Focus();
                e.Handled = true;
            }
        }
    }
}
